const express=require('express')
//创建路由器对象
//引入连接池
const pool=require('../pool.js')
var router=express.Router()
//添加路由
//1.用户注册
router.post('/reg',function(req,res){
  //获取到数据
  //console.log(req.body)
  //验证数据是否为空
  if (!req.body.uname)
      {
      res.send({code:401,msg:'uname required'})
     //阻止往后执行
     return;
  
     }
    if (!req.body.upwd)
    {
	res.send({code:402,msg:'upwd required'})
   return
    }
     if (!req.body.email)
     {
	res.send({code:403,msg:'email required'})
 return
	}
 if (!req.body.phone)
 {
	res.send({code:404,msg:'phone required'})
		return
 }
 //把数据插入到数据库
 //执行SQL语句
  pool.query('INSERT INTO xz_user SET ? ',[req.body],function(err,result){
  if(err) throw err
	  if (result.affectedRows>0)
	  {
 res.send({code:200,msg:'register suc'})
	  }
  })

  
 })


//2.用户登录
router.post('/login',function(req,res){
  console.log(req.body)
  //验证数据是否为空
if (!req.body.uname)
 {
        res.send({code:406,msg:'uname required'})
        return
	}
if (!req.body.upwd)
{
	res.send({code:407,msg:'upwd required'})
		return
}
  //执行SQL语句
pool.query('SELECT * FROM xz_user WHERE uname=? AND upwd=? ',[req.body.uname,req.body.upwd],function(err,result){
if (err)throw err
	//req.send(result)
if (result.length>0)
{
	res.send({code:200,msg:'login suc'})
}else {
	res.send({code:301,msg:'login err'})
	}
})


})

router.get('/delete',function(req,res){

 if (!req.query.uid)
 {
	res.send({code:401,msg:'uid required'})
	return
	}
	
	pool.query('DELETE FROM xz_user WHERE uid=?',[req.query.uid],function(err,result){
	if(err)throw err;
    if (result.affectedRows>0)
	  {
 res.send({code:200,msg:'delete suc'})
	  }else {
	res.send({code:301,msg:'delete err'})
	}
	
	})

})

router.get('/detail',function(req,res){
if(!req.query.uid)
	{
res.send({code:401,msg:'uid required'})
	return
}
pool.query('SELECT * FROM xz_user WHERE uid=?',[req.query.uid],function(err,result){
if(err)throw err
	if(result.length>0){
  res.send(result)
}else{
  res.send('查无此人')
  }

})
})

router.get('/update',function(req,res){
//遍历队象中的属性，如果属性值为空，提示属性名这一项
var obj=req.query
	var num=400
	for (var key in obj )
	{
	num++
	}
if (!obj[key])
{
	res.send({code:num,msg:key +'  required'})
		return
}

/*if(!req.query.uid)
	{  
  res.send({code:403,msg:'uid required'})
  return
  }
  return
  }

   if (!req.query.phone)
   {
	res.send({code:404,msg:'phone required'})
		return
 }

  if (!req.query.user_name)
      {
      res.send({code:401,msg:'user_name required'})
     
       return;}
	   
  if (!req.query.gender)
      {
      res.send({code:401,msg:'gender required'})
     
       return;}*/
 pool.query('UPDATE xz_user SET email=?,phone=?,user_name=?,gender=? WHERE uid=?',[req.query.email,req.query.phone,req.query.user_name,req.query.gender,req.query.uid],function(err,result){
if(err)throw err
   if (result.affectedRows>0)
	  {
 res.send({code:200,msg:'update suc'})
	  }else {
	res.send({code:301,msg:'update err'})
	}
	

  })

  })


router.get('/list',function(req,res){
var pno=req.query.pno
var count=req.query.count

if(!pno) pno=1
if(!count) count=2
pno=parseInt(pno)
count=parseInt(count)
//console.log(pno,count)
var start=(pno-1)*count
pool.query('SELECT * FROM xz_user LIMIT ?,?',[start,count],function(err,result){
  if(err)throw err
  res.send(result)

})

})














//导出路由器
module.exports=router